package com.jk.controller.account;

import com.jk.dto.JsonResult;
import com.jk.dto.account.AccUserDTO;
import com.jk.entity.account.AccUser;
import com.jk.service.account.AccUserService;
import com.jk.util.JsonUtil;
import com.jk.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *  Created by yanghuan on 15-8-31.
 */
@Controller
@RequestMapping("/account/user")
public class UserController {
    @Resource
    private AccUserService accUserService;

    //跳转到用户管理页面
    @RequestMapping("/page")
    public String manage() {
        return "account/user/list";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public String getList(AccUserDTO accUserDTO) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("total", accUserService.total(accUserDTO));
            map.put("rows", accUserService.select(accUserDTO));
            return JsonUtil.toString(map);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new HashMap<String, Object>());
        }
    }

    @RequestMapping(value = "toSave")
    public String add(String accUserId, ModelMap modelMap, Integer page, Integer rows) {
        if (null != page) {
            modelMap.addAttribute("pageNumber", page);
        }
        if (null != rows) {
            modelMap.addAttribute("pageNumber", rows);
        }
        try {
            if (!StringUtil.isEmpty(accUserId)) {
                modelMap.addAttribute("accUserId", accUserId);
                AccUser accUser = accUserService.selectByUserId(accUserId);
                modelMap.addAttribute("accUser", accUser);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "account/user/edit";
    }

    @RequestMapping(value = "selectId")
    @ResponseBody
    public String selectId(String accUserId) {
        try {
            return JsonUtil.toString(accUserService.selectByUserId(accUserId));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new AccUser());
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(AccUserDTO accUserDTO) {
        try {
            accUserService.save(accUserDTO);
            return JsonUtil.toString(new JsonResult(200, "保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "保存失败"));
        }
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(String ids) {
        try {
            accUserService.delete(ids);
            return JsonUtil.toString(new JsonResult(200, "删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "删除失败"));
        }
    }

    @RequestMapping(value = "checkName")
    @ResponseBody
    public String checkName(String accUserName, String id) {
        try {
            AccUser accUser = accUserService.getByName(accUserName);
            if (null == accUser) {
                return JsonUtil.toString(new JsonResult(200, "用户名可用"));
            } else {
                if (id.equals(accUser.getAccUserId())) {
                    return JsonUtil.toString(new JsonResult(200, "用户名可用"));
                } else {
                    return JsonUtil.toString(new JsonResult(201, "用户名已存在"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.toString(new JsonResult(201, "操作失败"));
        }
    }
}
